<template>
  <div>
    <el-form ref="deptForm" :rules="rulesAdd" :model="form" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments } from '@/api/departments.js'
export default {
  name: '',
  props: {
    pid: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean
    },
    isEdit: {
      type: Boolean
    },
    // eslint-disable-next-line vue/require-default-prop
    row: {
      type: Object
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      employees: [],
      rulesAdd: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  // 监听编辑框状态，将父组件的数据赋值给子组件form
  watch: {
    isEdit: {
      handler(newVal, oldVal) {
        if (newVal) {
          // 深拷贝，数据是传递过来的，直接使用表单双向绑定会影响源头数据
          this.form = JSON
        }
      },
      immediate: true // 初次进入立即执行
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    async loadEmployee() {
      const { data } = await getEmployeeSimple()
      this.employees = data
    },
    // 点击确认
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        //   兜底效验
        if (valid) {
          // 点击确认添加部门
          // this.doAdd()
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doEdit() {
      const res = await updateDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('success') //  关闭对话框
      }
    },
    // 取消对话框
    hCancel() {
      this.$emit('updata:visible', false)
    },
    // 获取添加部门数据
    async doAdd() {
      // 将pid赋值给form中的pid
      this.form.pid = this.pid
      const res = await addDepartments(this.form)
      if (res.code === 10000) {
        //  表示添加部门成功
        // 子调用父组件函数
        this.$emit('success')
        // 清空数据
        this.close()
      }
    },
    // 请客数据
    close() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
